#include "headfile.h"

extern int flag_open;
extern float tem;
extern bool flag_show, flag_beep_pwm;

int time_beep = 0;
static bool flag_beep = false;

int set_tem = 30;

int fanv = 50;

void TIM7_Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7,ENABLE);
	
	TIM_TimeBaseInitTypeDef TIM7_Struct; // 0.2s
	TIM7_Struct.TIM_Prescaler = 8400 - 1;
	TIM7_Struct.TIM_CounterMode = TIM_CounterMode_Up;
	TIM7_Struct.TIM_Period = 2000 - 1;
	
	TIM_TimeBaseInit(TIM7,&TIM7_Struct);
	
	TIM_ITConfig(TIM7,TIM_IT_Update,ENABLE);
}

void TIM7_IRQHandler(void)
{
	if(SET == TIM_GetITStatus(TIM7,TIM_IT_Update))
	{
		TIM_ClearITPendingBit(TIM7,TIM_IT_Update);
		if(flag_open)
			togglLED(3);
		
		if(1 == flag_show)
		{
			if((int)tem >= set_tem && false == flag_beep_pwm)
			{
				//USART_SendData(USART1,115);
				flag_beep = true;
				TIM_SetCompare2(TIM12,fanv);
			}
			//else 
			else if((int)tem < set_tem)
			{
				if(5 < time_beep) 
					flag_beep = false;
				TIM_SetCompare2(TIM12,0);
			}

			if(true == flag_beep)
			{
				time_beep++;
				if(55 <= time_beep) 
				{
					flag_beep = false;
					time_beep = 0;
				}
			}
			else if(false == flag_beep && false == flag_beep_pwm) 
				time_beep = 0;
			
			if(true == flag_beep_pwm)
			{
				if(2 <= time_beep)
					time_beep = 0;
				else time_beep++;
				if(2 >= time_beep % 2 && 0 <= time_beep)
				{
					TIM_SetCompare3(TIM2, time_beep * 50);
				}
				else TIM_SetCompare3(TIM2, 0);
			}
			
		}
		else 	
		{
			flag_beep_pwm = false;
			time_beep = 0;
			flag_beep = false;
		}
	}
	
}
 